/**
 * Created by peter on 2017/2/22.
 */
var DatePlugin = {};


var datePlugin = {

    /**
     * 比较两个日期的大小, 日期靠后的大s
     * @param date1
     * @param date2
     * @returns {number} 0=第一个日期靠后, 1=同一个日期, 2=第二个日期靠后
     */
    compareDate: function(date1, date2) {

        if (!date1 || !date2) {
            return 3;
        } else {

            if (date1.length > date2.length) {
                date1 = date1.substr(0, date2.length);
            } else if (date1.length < date2.length) {
                date2 = date2.substr(0, date1.length);
            }
           
            // console.log(`${date1}对比${date2}`);

            var date1Arr = date1.split('-');
            var date2Arr = date2.split('-');

            date1 = date1Arr.join('');
            date2 = date2Arr.join('');


            if (date1 == date2) {
                return 1;
            } else if (date1 > date2) {
                return 0;
            } else if (date1 < date2) {
                return 2;
            }
        }
    },

    /**
     * 某个月的最后一天
     * @param year
     * @param month
     */
    monthOfLastDay: function(date) {
        var year = date.substr(0, 4);
        var month = date.substr(5, 2);
        var day = new Date(year, month, 0);
        return year + '-' + month + '-' + day.getDate(); //获取当月最后一天日期
    },

    /**
     * 两个日期之间月份差
     * @param date1
     * @param date2
     * @returns {number}
     */
    getMonthNumber: function(date1, date2) {

        if (!date1 || !date2) {
            return 0;
        }

        var date1Arr = date1.split('-');
        var date2Arr = date2.split('-');

        var year1 = date1Arr[0];
        var year2 = date2Arr[0];
        var month1 = date1Arr[1];
        var month2 = date2Arr[1];
        // 两个月份之间间隔的月份
        var len = (year2 - year1) * 12 + (month2 - month1);
        return len;
    },

    /**
     * 计算两个时间的日期差
     * @param date1
     * @param date2
     * @returns {number}
     */
    getDayNumber: function(date1, date2) {
        var year1 = date1.substr(0, 4);
        var year2 = date2.substr(0, 4);

        var month1 = date1.substr(5, 2);
        var month2 = date2.substr(5, 2);

        var day1 = date1.substr(8, 2);
        var day2 = date2.substr(8, 2);

        var temp1 = year1 + "/" + month1 + "/" + day1;
        var temp2 = year2 + "/" + month2 + "/" + day2;

        var dateaa = new Date(temp1);
        var datebb = new Date(temp2);
        var date = datebb.getTime() - dateaa.getTime();
        var time = Math.floor(date / (1000 * 60 * 60 * 24));

        return time;
    },


    /**
     * 获取当前时间 yyyy-MM-dd HH:mm:ss
     * @returns {string}
     */
    getNowFormartDate: function() {
        var date = new Date();
        var seperator1 = "-";
        var seperator2 = ":";
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }

        var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate +
            " " + date.getHours() + seperator2 + date.getMinutes() +
            seperator2 + date.getSeconds();
        return currentdate;
    },

    /**
     * 某个日期, n天后是多少号
     * @param expireDate
     * @param day
     * @constructor
     */
    dateAfterDayNumber: function(expireDate, day) {
        var date1 = new Date(expireDate);
        var date2 = new Date(date1);
        date2.setDate(date1.getDate() + day);

        var month = (date2.getMonth() + 1);

        month = month >= 10 ? month : "0" + month;

        var times = date2.getFullYear() + "-" + month + "-" + date2.getDate();
        return times;
    },
    /**
     * 某个日期, n月后是多少号
     * @param expireDate
     * @param day
     * @constructor
     */
    dateAfterMonthNumber: function(date, monthNum) {
        
        var dateArr = date.split('-');
        var year = dateArr[0]; //获取当前日期的年份
        var month = dateArr[1]; //获取当前日期的月份
        var day = dateArr[2]; //获取当前日期的日
        var days = new Date(year, month, 0);
        days = days.getDate(); //获取当前日期中的月的天数
        var year2 = year;
        var month2 = parseInt(month) + parseInt(monthNum);


        if (month2 > 12) {
            year2 = parseInt(year2) + parseInt((parseInt(month2) / 12 == 0 ? 1 : parseInt(month2) / 12));
            month2 = parseInt(month2) % 12;
        }
        
        var day2 = day;
        var days2 = new Date(year2, month2, 0);
        days2 = days2.getDate();
        if (day2 > days2) {
            day2 = days2;
        }
        if (month2 < 10) {
            month2 = '0' + month2;
        }

        if (month2 == 0) {
            year2 = year2 - 1;
            month2 = 12;
        }

        var t2 = year2 + '-' + month2 + '-' + day2;
        
        return t2;
    },

}

DatePlugin.install = function(Vue, options) {
    Vue.prototype.$datePlugin = datePlugin;
}

export default DatePlugin;